package com.wss.module.market.ui.goods.cart;

import com.wss.common.base.BaseActionBarAbility;
import com.wss.common.base.mvp.BasePresenter;
import com.wss.common.constants.Dic;
import com.wss.common.utils.JsonUtils;
import com.wss.common.utils.MoneyUtils;
import com.wss.common.utils.ToastUtils;
import com.wss.common.widget.MultipleItemView;
import com.wss.module.market.ResourceTable;
import com.wss.module.market.bean.Vendor;
import com.wss.module.market.ui.goods.cart.adapter.OrderSettlementGoodsAdapter;
import com.wss.module.market.utils.ShoppingCartUtils;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;

import java.util.ArrayList;
import java.util.List;

public class OrderSettlementAbility extends BaseActionBarAbility
        implements Component.ClickedListener {

    Text tvNoAddress;

    Text tvName;

    Text tvAddress;

    ListContainer rvGoodsList;

    MultipleItemView mivCoupon;

    MultipleItemView mivShoppingBi;

    Text tvGoodsPrice;

    Text tvCouponPrice;

    DependentLayout rlCouponMoney;

    DependentLayout rlShoppingBiMoney;

    Text tvShoppingBi;

    Text tvTotal;

    Text btnBuy;

    DependentLayout rlBottom;

    private List<Vendor> vendorList = new ArrayList<>();

    private double cartCountPrice;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        findView();
        initView();
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_order_settlement;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private void initView() {
        setCenterText("结算订单");
        //以JSON的形式传入商品信息
        String goodsJson = getIntent().getStringParam(Dic.VENDOR_GOODS_INFO);
        vendorList.addAll(JsonUtils.getList(goodsJson, Vendor.class));
        cartCountPrice = ShoppingCartUtils.getCartCountPrice(vendorList);

        rvGoodsList.setItemProvider(new OrderSettlementGoodsAdapter(context, vendorList, (data, position) -> {

            ToastUtils.show("商品清单");
        }));
//        mivShoppingBi.setRightIconChangedListener(checked -> {
//            if (checked) {
//                rlShoppingBiMoney.setVisibility(Component.VISIBLE);
//                cartCountPrice -= 10.00;
//            } else {
//                cartCountPrice += 10.00;
//                rlShoppingBiMoney.setVisibility(Component.HIDE);
//            }
//            setTotal();
//        });
        setTotal();
    }

    private void setTotal() {
        //减掉优惠券
        String subtract = MoneyUtils.Algorithm.subtract(String.valueOf(cartCountPrice), "100");
        tvTotal.setText(String.format("合计：%s", subtract));
    }

    private void findView() {
        tvNoAddress = (Text) findComponentById(ResourceTable.Id_tv_no_address);
        tvName = (Text) findComponentById(ResourceTable.Id_tv_name);
        tvAddress = (Text) findComponentById(ResourceTable.Id_tv_address);
        rvGoodsList = (ListContainer) findComponentById(ResourceTable.Id_rv_goods_list);
        mivCoupon = (MultipleItemView) findComponentById(ResourceTable.Id_miv_coupon);
        mivShoppingBi = (MultipleItemView) findComponentById(ResourceTable.Id_miv_shopping_bi);
        tvGoodsPrice = (Text) findComponentById(ResourceTable.Id_tv_goods_price);
        tvCouponPrice = (Text) findComponentById(ResourceTable.Id_tv_coupon_price);
        rlCouponMoney = (DependentLayout) findComponentById(ResourceTable.Id_rl_coupon_money);
        rlShoppingBiMoney = (DependentLayout) findComponentById(ResourceTable.Id_rl_shopping_bi_money);
        tvShoppingBi = (Text) findComponentById(ResourceTable.Id_tv_shopping_bi);
        tvTotal = (Text) findComponentById(ResourceTable.Id_tv_total);
        btnBuy = (Text) findComponentById(ResourceTable.Id_btn_buy);
        rlBottom = (DependentLayout) findComponentById(ResourceTable.Id_rl_bottom);

        tvNoAddress.setClickedListener(this::onClick);
        mivCoupon.setClickedListener(this::onClick);
        btnBuy.setClickedListener(this::onClick);

        mivShoppingBi.setSelected(true);
        mivShoppingBi.setRightIconChangedListener(checked -> {
            if (checked) {
                rlShoppingBiMoney.setVisibility(Component.VISIBLE);
                cartCountPrice -= 10.00;
                mivShoppingBi
                        .setRightIconResource(ResourceTable
                                .Media_list_checkbox_select);
            } else {
                cartCountPrice += 10.00;
                rlShoppingBiMoney.setVisibility(Component.HIDE);
                mivShoppingBi
                        .setRightIconResource(ResourceTable
                                .Media_list_checkbox_default);
            }
            setTotal();
        });
    }

    @Override
    public void onClick(Component view) {
        int id = view.getId();
        if (id == ResourceTable.Id_tv_no_address) {
            //去添加地址
            ToastUtils.show("去添加地址");
        } else if (id == ResourceTable.Id_miv_coupon) {
            //优惠券
            ToastUtils.show("优惠券页面");
        } else if (id == ResourceTable.Id_btn_buy) {
            //去支付
            ToastUtils.show("去支付");
        }
    }
}
